package cn.mesmile.lock.service;

import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author zb
 * @date 2020/12/4 14:33
 * @Description
 */
@AllArgsConstructor
@Service
public class RangeService {

    private final RedisService redisService;

    private static final String RANK_NAME = "user_score";


    /**
     *  第一次添加分数
     */
    public boolean addScore(String uid,Double value){
        return redisService.zAdd(RANK_NAME, uid, value);
    }

    /**
     * 从小到大排序
     * @return
     */
    public Set<ZSetOperations.TypedTuple<Object>>  getRankKeyValue(){
        Set<ZSetOperations.TypedTuple<Object>> typedTuples = redisService.zRangeWithScores(RANK_NAME, 0, -1);

        return typedTuples;
    }


    public long getRank(Object value) {
        long l = redisService.zRank(RANK_NAME, value);
        return l;
    }

    public long getReverseRank(Object value){
        long l = redisService.reverseZRank(RANK_NAME, value);
        return l;
    }

    public double zIncrby(Object value,Double score){
        double v = redisService.zIncrby(RANK_NAME, value, score);
        return v;
    }
}
